package com.bignerdranch.android.expresshelper.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.bignerdranch.android.expresshelper.R;
import com.bignerdranch.android.expresshelper.Utils.MyImageCache;
import com.bignerdranch.android.expresshelper.Utils.RequestUtil;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/6/27.
 */
public class CompanyListAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private JSONArray mDate;

    public CompanyListAdapter(Context context, JSONArray date) {
        mContext = context;
        mDate = date;
        mInflater = LayoutInflater.from(context);
    }

    public String getUrl(int index){
        try {
            return mDate.getJSONObject(index).getString("imgUrl");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void addDate(JSONArray date){
        for(int i = 0;i <date.length() ;i++){
            try {
                mDate.put(date.getJSONObject(i));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public int getCount() {
        return mDate.length();
    }

    @Override
    public Object getItem(int position) {
        try {
            return mDate.getJSONObject(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.dialog_company,null);
        }
        ViewHandler handler = new ViewHandler();

        if (convertView.getTag() != null){
            handler = (ViewHandler) convertView.getTag();
        }else {
            handler.name = (TextView)convertView.findViewById(R.id.company_name);
            handler.simName = (TextView)convertView.findViewById(R.id.company_sim);
            handler.img = (NetworkImageView) convertView.findViewById(R.id.company_icon);

            convertView.setTag(handler);
        }

        try {
            JSONObject jsonObject = mDate.getJSONObject(position);
            handler.name.setText(jsonObject.getString("expName"));
            handler.simName.setText(jsonObject.getString("simpleName"));
            ImageLoader loader = new ImageLoader(RequestUtil.getRequestQueue(mContext),new MyImageCache());
            handler.img.setDefaultImageResId(R.drawable.anim_ro);
            handler.img.setErrorImageResId(R.drawable.waiting);
            handler.img.setImageUrl(jsonObject.getString("imgUrl"),loader);
           /* Picasso.with(mContext).load(jsonObject.getString("imgUrl"))
                    .error(R.drawable.waiting).placeholder(R.drawable.anim_ro)
                    .memoryPolicy(MemoryPolicy.NO_STORE).into(handler.img);*/

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return convertView;
    }

    private static class ViewHandler{
        public NetworkImageView img;
        public TextView name;
        public TextView simName;

    }
}
